/**
 * [47] 全排列 II
 *
 * @format
 * @lc app=leetcode.cn id=47 lang=javascript
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
// 递归遍历
var permuteUnique = function (nums) {
    const dist = {}
    nums.forEach(num => (dist[num] == undefined ? (dist[num] = 1) : dist[num]++))
    function permute(dist) {
        // console.log("dist:", dist)
        const res = []
        for (let key in dist) {
            if (dist[key] == 0) continue
            const temp = { ...dist }
            temp[key]--
            // console.log("temp:", temp)
            res.push(...permute(temp).map(seq => [key, ...seq]))
            // console.log("res:", res)
        }
        if (res.length == 0) res.push([])
        return res
    }
    return permute(dist)
}
// debugger
// const nums = [1, 1, 2]
// const res = permuteUnique(nums)
// console.log(...res)
// @lc code=end
